package cn.edu.imut.dao;


import cn.edu.imut.bean.OrderBean;
import cn.edu.imut.util.DBUtil;

import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {

    // 添加订单
    public int addOrder(OrderBean order) throws SQLException {
        String sql = "INSERT INTO gs_orders (product_id, user_id, order_status, order_time, total_amount, product_quantity, address, payment_time) VALUES (?,?,?,?,?,?,?,?)";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pstmt.setInt(1, order.getProductId());
            pstmt.setInt(2, order.getUserId());
            pstmt.setString(3, order.getOrderStatue());
            pstmt.setObject(4, order.getOrderTime());
            pstmt.setDouble(5, order.getTotalAmount());
            pstmt.setInt(6, order.getProsuctQuentity());
            pstmt.setString(7, order.getAddress());
            pstmt.setObject(8, order.getPaymentTime());
            pstmt.executeUpdate();
            ResultSet rs =pstmt.getGeneratedKeys();
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,pstmt);
        }
        return -1;
    }

    // 根据订单ID查询订单
    public OrderBean getOrderById(int orderId) throws SQLException {
        String sql = "SELECT * FROM gs_orders WHERE order_id =?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, orderId);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                int order_id = rs.getInt("order_id");
                int product_id = rs.getInt("product_id");
                int user_id = rs.getInt("user_id");
                String order_status = rs.getString("order_status");
                LocalDateTime order_time = rs.getObject("order_time", LocalDateTime.class);
                double total_amount = rs.getDouble("total_amount");
                int product_quantity = rs.getInt("product_quantity");
                String address = rs.getString("address");
                LocalDateTime payment_time = rs.getObject("payment_time", LocalDateTime.class);
                return new OrderBean(order_id, product_id, user_id, order_status, order_time, total_amount, product_quantity, address, payment_time);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close( conn,pstmt,rs);
        }
        return null;
    }

    // 查询所有订单
    public List<OrderBean> getAllOrders() throws SQLException {
        List<OrderBean> orders = new ArrayList<>();
        String sql = "SELECT * FROM gs_orders";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                int order_id = rs.getInt("order_id");
                int product_id = rs.getInt("product_id");
                int user_id = rs.getInt("user_id");
                String order_status = rs.getString("order_status");
                LocalDateTime order_time = rs.getObject("order_time", LocalDateTime.class);
                double total_amount = rs.getDouble("total_amount");
                int product_quantity = rs.getInt("product_quantity");
                String address = rs.getString("address");
                LocalDateTime payment_time = rs.getObject("payment_time", LocalDateTime.class);
                OrderBean order = new OrderBean(order_id, product_id, user_id, order_status, order_time, total_amount, product_quantity, address, payment_time);
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,pstmt,rs);
        }
        return orders;
    }

    // 更新订单
    public void updateOrder(OrderBean order) throws SQLException {
        String sql = "UPDATE gs_orders SET product_id =?, user_id =?, order_status =?, order_time =?, total_amount =?, product_quantity =?, address =?, payment_time =? WHERE order_id =?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, order.getProductId());
            pstmt.setInt(2, order.getUserId());
            pstmt.setString(3, order.getOrderStatue());
            pstmt.setObject(4, order.getOrderTime());
            pstmt.setDouble(5, order.getTotalAmount());
            pstmt.setInt(6, order.getProsuctQuentity());
            pstmt.setString(7, order.getAddress());
            pstmt.setObject(8, order.getPaymentTime());
            pstmt.setInt(9, order.getOrder_id());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,pstmt);
        }
    }

    // 删除订单
    public void deleteOrder(int orderId) throws SQLException {
        String sql = "DELETE FROM gs_orders WHERE order_id =?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, orderId);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close( conn,pstmt);
        }
    }
}
